package com.md.utils;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

@ControllerAdvice
public class GlobalControllerAdvice {
//全局异常处理
    private final String ERROR = "error";
//设定全局处理的异常
    @ExceptionHandler(value=PermissionException.class)
    public ModelAndView noPermission(PermissionException e){
        //new一个返回页面为error
        ModelAndView modelAndView = new ModelAndView(ERROR);
        //绑定：将异常中的message信息与error页面绑定，也就是说当抛出PermissionException异常时,就返回error页面
        modelAndView.addObject(ERROR,e.getMessage());
        return modelAndView;
    }

    @ExceptionHandler(value=RuntimeException.class)
    @ResponseBody
    public Map<String,Object> runtimeException(RuntimeException e){
        e.printStackTrace();
        return MapControll.getInstance().error().getMap();
    }


}
